{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用示例选择器\n",
    "\n",
    "- 我们将重用上一节中的示例集和格式化程序。但是，我们不会将示例直接输入到FewShotPromptTemplate对象中，而是将它们输入到名为SemanticSimilarityExampleSelector实例的ExampleSelector的实现中。此类根据它们与输入的相似性从初始集中选择少量示例。它使用嵌入模型来计算输入和少量示例之间的相似性，以及向量存储来执行最邻近搜索。\n",
    "\n",
    "- 为了展示它的样子，让我们初始化一个实例并单独调用它："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: langchain in .\\.venv\\lib\\site-packages (0.2.7)\n",
      "Requirement already satisfied: langchain_community in .\\.venv\\lib\\site-packages (0.2.7)\n",
      "Requirement already satisfied: langchain_openai in .\\.venv\\lib\\site-packages (0.1.14)\n",
      "Requirement already satisfied: langchain_chroma in .\\.venv\\lib\\site-packages (0.1.2)\n",
      "Requirement already satisfied: PyYAML>=5.3 in .\\.venv\\lib\\site-packages (from langchain) (6.0.1)\n",
      "Requirement already satisfied: SQLAlchemy<3,>=1.4 in .\\.venv\\lib\\site-packages (from langchain) (2.0.31)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in .\\.venv\\lib\\site-packages (from langchain) (3.9.5)\n",
      "Requirement already satisfied: langchain-core<0.3.0,>=0.2.12 in .\\.venv\\lib\\site-packages (from langchain) (0.2.12)\n",
      "Requirement already satisfied: langchain-text-splitters<0.3.0,>=0.2.0 in .\\.venv\\lib\\site-packages (from langchain) (0.2.2)\n",
      "Requirement already satisfied: langsmith<0.2.0,>=0.1.17 in .\\.venv\\lib\\site-packages (from langchain) (0.1.84)\n",
      "Requirement already satisfied: numpy<2,>=1 in .\\.venv\\lib\\site-packages (from langchain) (1.26.4)\n",
      "Requirement already satisfied: pydantic<3,>=1 in .\\.venv\\lib\\site-packages (from langchain) (2.8.2)\n",
      "Requirement already satisfied: requests<3,>=2 in .\\.venv\\lib\\site-packages (from langchain) (2.32.3)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in .\\.venv\\lib\\site-packages (from langchain) (8.5.0)\n",
      "Requirement already satisfied: dataclasses-json<0.7,>=0.5.7 in .\\.venv\\lib\\site-packages (from langchain_community) (0.6.7)\n",
      "Requirement already satisfied: openai<2.0.0,>=1.32.0 in .\\.venv\\lib\\site-packages (from langchain_openai) (1.35.10)\n",
      "Requirement already satisfied: tiktoken<1,>=0.7 in .\\.venv\\lib\\site-packages (from langchain_openai) (0.7.0)\n",
      "Requirement already satisfied: chromadb<0.6.0,>=0.4.0 in .\\.venv\\lib\\site-packages (from langchain_chroma) (0.5.3)\n",
      "Requirement already satisfied: fastapi<1,>=0.95.2 in .\\.venv\\lib\\site-packages (from langchain_chroma) (0.111.0)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in .\\.venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n",
      "Requirement already satisfied: attrs>=17.3.0 in .\\.venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.2.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in .\\.venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in .\\.venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in .\\.venv\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)\n",
      "Requirement already satisfied: build>=1.0.3 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.2.1)\n",
      "Requirement already satisfied: chroma-hnswlib==0.7.3 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.7.3)\n",
      "Requirement already satisfied: uvicorn[standard]>=0.18.3 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.30.1)\n",
      "Requirement already satisfied: posthog>=2.4.0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (3.5.0)\n",
      "Requirement already satisfied: typing-extensions>=4.5.0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (4.12.2)\n",
      "Requirement already satisfied: onnxruntime>=1.14.1 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.18.1)\n",
      "Requirement already satisfied: opentelemetry-api>=1.2.0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.25.0)\n",
      "Requirement already satisfied: opentelemetry-exporter-otlp-proto-grpc>=1.2.0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.25.0)\n",
      "Requirement already satisfied: opentelemetry-instrumentation-fastapi>=0.41b0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.46b0)\n",
      "Requirement already satisfied: opentelemetry-sdk>=1.2.0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.25.0)\n",
      "Requirement already satisfied: tokenizers>=0.13.2 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.19.1)\n",
      "Requirement already satisfied: pypika>=0.48.9 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.48.9)\n",
      "Requirement already satisfied: tqdm>=4.65.0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (4.66.4)\n",
      "Requirement already satisfied: overrides>=7.3.1 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (7.7.0)\n",
      "Requirement already satisfied: importlib-resources in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (6.4.0)\n",
      "Requirement already satisfied: grpcio>=1.58.0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.64.1)\n",
      "Requirement already satisfied: bcrypt>=4.0.1 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (4.1.3)\n",
      "Requirement already satisfied: typer>=0.9.0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.12.3)\n",
      "Requirement already satisfied: kubernetes>=28.1.0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (30.1.0)\n",
      "Requirement already satisfied: mmh3>=4.0.1 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (4.1.0)\n",
      "Requirement already satisfied: orjson>=3.9.12 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (3.10.6)\n",
      "Requirement already satisfied: httpx>=0.27.0 in .\\.venv\\lib\\site-packages (from chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.27.0)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in .\\.venv\\lib\\site-packages (from dataclasses-json<0.7,>=0.5.7->langchain_community) (3.21.3)\n",
      "Requirement already satisfied: typing-inspect<1,>=0.4.0 in .\\.venv\\lib\\site-packages (from dataclasses-json<0.7,>=0.5.7->langchain_community) (0.9.0)\n",
      "Requirement already satisfied: starlette<0.38.0,>=0.37.2 in .\\.venv\\lib\\site-packages (from fastapi<1,>=0.95.2->langchain_chroma) (0.37.2)\n",
      "Requirement already satisfied: fastapi-cli>=0.0.2 in .\\.venv\\lib\\site-packages (from fastapi<1,>=0.95.2->langchain_chroma) (0.0.4)\n",
      "Requirement already satisfied: jinja2>=2.11.2 in .\\.venv\\lib\\site-packages (from fastapi<1,>=0.95.2->langchain_chroma) (3.1.4)\n",
      "Requirement already satisfied: python-multipart>=0.0.7 in .\\.venv\\lib\\site-packages (from fastapi<1,>=0.95.2->langchain_chroma) (0.0.9)\n",
      "Requirement already satisfied: ujson!=4.0.2,!=4.1.0,!=4.2.0,!=4.3.0,!=5.0.0,!=5.1.0,>=4.0.1 in .\\.venv\\lib\\site-packages (from fastapi<1,>=0.95.2->langchain_chroma) (5.10.0)\n",
      "Requirement already satisfied: email_validator>=2.0.0 in .\\.venv\\lib\\site-packages (from fastapi<1,>=0.95.2->langchain_chroma) (2.2.0)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in .\\.venv\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.12->langchain) (1.33)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in .\\.venv\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.12->langchain) (24.1)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in .\\.venv\\lib\\site-packages (from openai<2.0.0,>=1.32.0->langchain_openai) (4.4.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in .\\.venv\\lib\\site-packages (from openai<2.0.0,>=1.32.0->langchain_openai) (1.9.0)\n",
      "Requirement already satisfied: sniffio in .\\.venv\\lib\\site-packages (from openai<2.0.0,>=1.32.0->langchain_openai) (1.3.1)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in .\\.venv\\lib\\site-packages (from pydantic<3,>=1->langchain) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.20.1 in .\\.venv\\lib\\site-packages (from pydantic<3,>=1->langchain) (2.20.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in .\\.venv\\lib\\site-packages (from requests<3,>=2->langchain) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in .\\.venv\\lib\\site-packages (from requests<3,>=2->langchain) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in .\\.venv\\lib\\site-packages (from requests<3,>=2->langchain) (2.2.2)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in .\\.venv\\lib\\site-packages (from requests<3,>=2->langchain) (2024.7.4)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in .\\.venv\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)\n",
      "Requirement already satisfied: regex>=2022.1.18 in .\\.venv\\lib\\site-packages (from tiktoken<1,>=0.7->langchain_openai) (2024.5.15)\n",
      "Requirement already satisfied: pyproject_hooks in .\\.venv\\lib\\site-packages (from build>=1.0.3->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.1.0)\n",
      "Requirement already satisfied: colorama in .\\.venv\\lib\\site-packages (from build>=1.0.3->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.4.6)\n",
      "Requirement already satisfied: dnspython>=2.0.0 in .\\.venv\\lib\\site-packages (from email_validator>=2.0.0->fastapi<1,>=0.95.2->langchain_chroma) (2.6.1)\n",
      "Requirement already satisfied: httpcore==1.* in .\\.venv\\lib\\site-packages (from httpx>=0.27.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.0.5)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in .\\.venv\\lib\\site-packages (from httpcore==1.*->httpx>=0.27.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.14.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in .\\.venv\\lib\\site-packages (from jinja2>=2.11.2->fastapi<1,>=0.95.2->langchain_chroma) (2.1.5)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in .\\.venv\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.12->langchain) (3.0.0)\n",
      "Requirement already satisfied: six>=1.9.0 in .\\.venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.16.0)\n",
      "Requirement already satisfied: python-dateutil>=2.5.3 in .\\.venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (2.9.0.post0)\n",
      "Requirement already satisfied: google-auth>=1.0.1 in .\\.venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (2.32.0)\n",
      "Requirement already satisfied: websocket-client!=0.40.0,!=0.41.*,!=0.42.*,>=0.32.0 in .\\.venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.8.0)\n",
      "Requirement already satisfied: requests-oauthlib in .\\.venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (2.0.0)\n",
      "Requirement already satisfied: oauthlib>=3.2.2 in .\\.venv\\lib\\site-packages (from kubernetes>=28.1.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (3.2.2)\n",
      "Requirement already satisfied: coloredlogs in .\\.venv\\lib\\site-packages (from onnxruntime>=1.14.1->chromadb<0.6.0,>=0.4.0->langchain_chroma) (15.0.1)\n",
      "Requirement already satisfied: flatbuffers in .\\.venv\\lib\\site-packages (from onnxruntime>=1.14.1->chromadb<0.6.0,>=0.4.0->langchain_chroma) (24.3.25)\n",
      "Requirement already satisfied: protobuf in .\\.venv\\lib\\site-packages (from onnxruntime>=1.14.1->chromadb<0.6.0,>=0.4.0->langchain_chroma) (4.25.3)\n",
      "Requirement already satisfied: sympy in .\\.venv\\lib\\site-packages (from onnxruntime>=1.14.1->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.13.0)\n",
      "Requirement already satisfied: deprecated>=1.2.6 in .\\.venv\\lib\\site-packages (from opentelemetry-api>=1.2.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.2.14)\n",
      "Requirement already satisfied: importlib-metadata<=7.1,>=6.0 in .\\.venv\\lib\\site-packages (from opentelemetry-api>=1.2.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (7.1.0)\n",
      "Requirement already satisfied: googleapis-common-protos~=1.52 in .\\.venv\\lib\\site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.63.2)\n",
      "Requirement already satisfied: opentelemetry-exporter-otlp-proto-common==1.25.0 in .\\.venv\\lib\\site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.25.0)\n",
      "Requirement already satisfied: opentelemetry-proto==1.25.0 in .\\.venv\\lib\\site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.25.0)\n",
      "Requirement already satisfied: opentelemetry-instrumentation-asgi==0.46b0 in .\\.venv\\lib\\site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.46b0)\n",
      "Requirement already satisfied: opentelemetry-instrumentation==0.46b0 in .\\.venv\\lib\\site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.46b0)\n",
      "Requirement already satisfied: opentelemetry-semantic-conventions==0.46b0 in .\\.venv\\lib\\site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.46b0)\n",
      "Requirement already satisfied: opentelemetry-util-http==0.46b0 in .\\.venv\\lib\\site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.46b0)\n",
      "Requirement already satisfied: setuptools>=16.0 in .\\.venv\\lib\\site-packages (from opentelemetry-instrumentation==0.46b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (65.5.0)\n",
      "Requirement already satisfied: wrapt<2.0.0,>=1.0.0 in .\\.venv\\lib\\site-packages (from opentelemetry-instrumentation==0.46b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.16.0)\n",
      "Requirement already satisfied: asgiref~=3.0 in .\\.venv\\lib\\site-packages (from opentelemetry-instrumentation-asgi==0.46b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (3.8.1)\n",
      "Requirement already satisfied: monotonic>=1.5 in .\\.venv\\lib\\site-packages (from posthog>=2.4.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.6)\n",
      "Requirement already satisfied: backoff>=1.10.0 in .\\.venv\\lib\\site-packages (from posthog>=2.4.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (2.2.1)\n",
      "Requirement already satisfied: huggingface-hub<1.0,>=0.16.4 in .\\.venv\\lib\\site-packages (from tokenizers>=0.13.2->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.23.4)\n",
      "Requirement already satisfied: click>=8.0.0 in .\\.venv\\lib\\site-packages (from typer>=0.9.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (8.1.7)\n",
      "Requirement already satisfied: shellingham>=1.3.0 in .\\.venv\\lib\\site-packages (from typer>=0.9.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.5.4)\n",
      "Requirement already satisfied: rich>=10.11.0 in .\\.venv\\lib\\site-packages (from typer>=0.9.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (13.7.1)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in .\\.venv\\lib\\site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain_community) (1.0.0)\n",
      "Requirement already satisfied: httptools>=0.5.0 in .\\.venv\\lib\\site-packages (from uvicorn[standard]>=0.18.3->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.6.1)\n",
      "Requirement already satisfied: python-dotenv>=0.13 in .\\.venv\\lib\\site-packages (from uvicorn[standard]>=0.18.3->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.0.1)\n",
      "Requirement already satisfied: watchfiles>=0.13 in .\\.venv\\lib\\site-packages (from uvicorn[standard]>=0.18.3->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.22.0)\n",
      "Requirement already satisfied: websockets>=10.4 in .\\.venv\\lib\\site-packages (from uvicorn[standard]>=0.18.3->chromadb<0.6.0,>=0.4.0->langchain_chroma) (12.0)\n",
      "Requirement already satisfied: cachetools<6.0,>=2.0.0 in .\\.venv\\lib\\site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (5.3.3)\n",
      "Requirement already satisfied: pyasn1-modules>=0.2.1 in .\\.venv\\lib\\site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.4.0)\n",
      "Requirement already satisfied: rsa<5,>=3.1.4 in .\\.venv\\lib\\site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (4.9)\n",
      "Requirement already satisfied: filelock in .\\.venv\\lib\\site-packages (from huggingface-hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb<0.6.0,>=0.4.0->langchain_chroma) (3.15.4)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in .\\.venv\\lib\\site-packages (from huggingface-hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb<0.6.0,>=0.4.0->langchain_chroma) (2024.6.1)\n",
      "Requirement already satisfied: zipp>=0.5 in .\\.venv\\lib\\site-packages (from importlib-metadata<=7.1,>=6.0->opentelemetry-api>=1.2.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (3.19.2)\n",
      "Requirement already satisfied: markdown-it-py>=2.2.0 in .\\.venv\\lib\\site-packages (from rich>=10.11.0->typer>=0.9.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (3.0.0)\n",
      "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in .\\.venv\\lib\\site-packages (from rich>=10.11.0->typer>=0.9.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (2.18.0)\n",
      "Requirement already satisfied: humanfriendly>=9.1 in .\\.venv\\lib\\site-packages (from coloredlogs->onnxruntime>=1.14.1->chromadb<0.6.0,>=0.4.0->langchain_chroma) (10.0)\n",
      "Requirement already satisfied: mpmath<1.4,>=1.1.0 in .\\.venv\\lib\\site-packages (from sympy->onnxruntime>=1.14.1->chromadb<0.6.0,>=0.4.0->langchain_chroma) (1.3.0)\n",
      "Requirement already satisfied: pyreadline3 in .\\.venv\\lib\\site-packages (from humanfriendly>=9.1->coloredlogs->onnxruntime>=1.14.1->chromadb<0.6.0,>=0.4.0->langchain_chroma) (3.4.1)\n",
      "Requirement already satisfied: mdurl~=0.1 in .\\.venv\\lib\\site-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer>=0.9.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.1.2)\n",
      "Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in .\\.venv\\lib\\site-packages (from pyasn1-modules>=0.2.1->google-auth>=1.0.1->kubernetes>=28.1.0->chromadb<0.6.0,>=0.4.0->langchain_chroma) (0.6.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 23.1.2 -> 24.1.2\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "%pip install langchain langchain_community langchain_openai langchain_chroma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "examples = [\n",
    "    {\n",
    "        \"question\": \"谁活得更久，穆罕默德·阿里还是艾伦·图灵？\",\n",
    "        \"answer\": \"\"\"\n",
    "这里需要后续问题吗：是的。\n",
    "跟进：穆罕默德·阿里去世时多大？\n",
    "中级答案：穆罕默德·阿里去世时74岁。\n",
    "跟进：艾伦·图灵去世时多大？\n",
    "中级答案：艾伦·图灵去世时41岁。\n",
    "所以最后的答案是：穆罕默德·阿里\n",
    "\"\"\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"Craigslist的创始人是什么时候出生的？\",\n",
    "        \"answer\": \"\"\"\n",
    "这里需要后续问题吗：是的。\n",
    "跟进：谁是craigslist的创始人？\n",
    "中级答案：Craigslist是由Craig Newmark创立的。\n",
    "跟进：克雷格·纽马克是什么时候出生的？\n",
    "中级答案：克雷格·纽马克出生于1952年12月6日。\n",
    "所以最终答案是：1952年12月6日\n",
    "\"\"\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"乔治·华盛顿的外祖父是谁？\",\n",
    "        \"answer\": \"\"\"\n",
    "这里需要后续问题吗：是的。\n",
    "追问：乔治·华盛顿的母亲是谁？\n",
    "中级答案：乔治·华盛顿的母亲是玛丽·鲍尔·华盛顿。\n",
    "追问：玛丽·鲍尔·华盛顿的父亲是谁？\n",
    "中级答案：玛丽·鲍尔·华盛顿的父亲是约瑟夫·鲍尔。\n",
    "所以最后的答案是：约瑟夫·鲍尔\n",
    "\"\"\",\n",
    "    },\n",
    "    {\n",
    "        \"question\": \"大白鲨和皇家赌场的董事来自同一个国家吗？\",\n",
    "        \"answer\": \"\"\"\n",
    "这里需要后续问题吗：是的。\n",
    "跟进：《大白鲨》的导演是谁？\n",
    "中级答案：《大白鲨》的导演是史蒂文·斯皮尔伯格。\n",
    "跟进：史蒂文·斯皮尔伯格来自哪里？\n",
    "中级答案：美国。\n",
    "跟进：皇家赌场的董事是谁？\n",
    "中级答案：皇家赌场的导演是马丁·坎贝尔。\n",
    "跟进：马丁·坎贝尔来自哪里？\n",
    "中级答案：新西兰。\n",
    "所以最后的答案是：不\n",
    "\"\"\",\n",
    "    },\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "与输入最相似的示例: 大白鲨的导演是那个国家的？\n",
      "\n",
      "\n",
      "answer: \n",
      "这里需要后续问题吗：是的。\n",
      "跟进：《大白鲨》的导演是谁？\n",
      "中级答案：《大白鲨》的导演是史蒂文·斯皮尔伯格。\n",
      "跟进：史蒂文·斯皮尔伯格来自哪里？\n",
      "中级答案：美国。\n",
      "跟进：皇家赌场的董事是谁？\n",
      "中级答案：皇家赌场的导演是马丁·坎贝尔。\n",
      "跟进：马丁·坎贝尔来自哪里？\n",
      "中级答案：新西兰。\n",
      "所以最后的答案是：不\n",
      "\n",
      "question: 大白鲨和皇家赌场的董事来自同一个国家吗？\n"
     ]
    }
   ],
   "source": [
    "from langchain_chroma import Chroma\n",
    "from langchain_core.example_selectors import SemanticSimilarityExampleSelector\n",
    "from langchain_openai import OpenAIEmbeddings\n",
    "\n",
    "example_selector = SemanticSimilarityExampleSelector.from_examples(\n",
    "    # This is the list of examples available to select from.\n",
    "    examples,\n",
    "    # This is the embedding class used to produce embeddings which are used to measure semantic similarity.\n",
    "    OpenAIEmbeddings(),\n",
    "    # This is the VectorStore class that is used to store the embeddings and do a similarity search over.\n",
    "    Chroma,\n",
    "    # This is the number of examples to produce.\n",
    "    k=1,\n",
    ")\n",
    "\n",
    "# Select the most similar example to the input.\n",
    "# question = \"玛丽·鲍尔·华盛顿的父亲是谁？\"\n",
    "question = \"大白鲨的导演是那个国家的？\"\n",
    "selected_examples = example_selector.select_examples({\"question\": question})\n",
    "print(f\"与输入最相似的示例: {question}\")\n",
    "for example in selected_examples:\n",
    "    print(\"\\n\")\n",
    "    for k, v in example.items():\n",
    "        print(f\"{k}: {v}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "\n",
    "prompt_template = PromptTemplate.from_template(\"Question: {question}\\n{answer}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: 大白鲨和皇家赌场的董事来自同一个国家吗？\n",
      "\n",
      "这里需要后续问题吗：是的。\n",
      "跟进：《大白鲨》的导演是谁？\n",
      "中级答案：《大白鲨》的导演是史蒂文·斯皮尔伯格。\n",
      "跟进：史蒂文·斯皮尔伯格来自哪里？\n",
      "中级答案：美国。\n",
      "跟进：皇家赌场的董事是谁？\n",
      "中级答案：皇家赌场的导演是马丁·坎贝尔。\n",
      "跟进：马丁·坎贝尔来自哪里？\n",
      "中级答案：新西兰。\n",
      "所以最后的答案是：不\n",
      "\n",
      "\n",
      "Question: 大白鲨的导演是那个国家的？\n"
     ]
    }
   ],
   "source": [
    "from langchain_core.prompts import FewShotPromptTemplate\n",
    "\n",
    "prompt = FewShotPromptTemplate(\n",
    "    example_selector=example_selector,\n",
    "    example_prompt=prompt_template,\n",
    "    suffix=\"Question: {input}\",\n",
    "    input_variables=[\"input\"],\n",
    ")\n",
    "\n",
    "print(\n",
    "    prompt.invoke({\"input\": \"大白鲨的导演是那个国家的？\"}).to_string()\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
